<?php

/**
 * ShopEx licence
 *
 * @copyright  Copyright (c) 2005-2010 ShopEx Technologies Inc. (http://www.shopex.cn)
 * @license  http://ecos.shopex.cn/ ShopEx License
 */
class importexport_tasks_runimportdisable extends base_task_abstract implements base_interface_task
{

    const TTL = 86400;//缓存时间
    const REDIS_DB = 1;

    public function exec($params = null)
    {
        set_time_limit(3600);
        $taskModel = app::get('importexport')->model('task');
        $taskModel->update(array('status' => 4), array('key' => $params['key']));//执行队列，更新队列状态

//        $redis = kernel::single('base_kvstore_redis2');
//        $redis->selects(self::REDIS_DB);


        $redis = new Redis();
        $redis->connect('127.0.0.1');
        $redis->select(1);


        $objGoods = kernel::single('b2c_mdl_goods');
        $count = $redis->lLen($params['key']);
        $message = '';
        if($count > 0){
            for ($i = 0; $i < $count; $i++) {
                $rows = $redis->rpop($params['key']);
                $msg = null;
                $goods_id = $objGoods->getGoodsIdByBn($rows);
                $goods_id = intval($goods_id[0]);
                if($goods_id <= 0){
                    $message = 'bn为'.$rows.'的商品不存在'.chr(13).chr(10).$message;
                }else {
                    $finderResult['goods_id'][] = $goods_id;
                    $redis->lPush($params['key'] . '_complate', serialize($rows));
                    $redis->expire($params['key'] . '_complate', self::TTL);
                }
            }
            $finderResult = array_filter($finderResult);
            if(empty($finderResult)){
                $message = '没有匹配的项目';
            }else{
                $rs = $objGoods->setEnabled($finderResult,'false',$message);
            }

        }else{
            $message = '没有匹配的项目';
        }


        if ($message) {
            if ($redis->lLen($params['key'] . '_complate')) {
                $params['status'] = $params['status'] = 8;
            } else {
                $params['status'] = $params['status'] = 6;
            }
        } else {
            $params['status'] = $params['status'] = 5;
        }
        if ($message) {
            $redis->set($params['key'] . '_error_message', $message);
        }

        $msg = '共' . $count . '条,' . '完成' . $redis->lLen($params['key'] . '_complate') . '条';
        $taskModel->update(array('status' => $params['status'], 'complete_date' => time(), 'message' => $msg), array('key' => $params['key']));
    }

}
